//
//  VisitorView.swift
//  XNWeiBo_Swift
//
//  Created by xunan on 16/6/19.
//  Copyright © 2016年 xunan. All rights reserved.
//

import UIKit

// Swift中如何定义协议 必须遵守NSObjectProtocol
protocol VisitorViewDelegate: NSObjectProtocol {
    // 登录回调
    func loginBtnWillClick()
    // 注册回调
    func registerBtnWillClick()

}

class VisitorView: UIView {
    
    // 定义一个属性保存代理对象 weak避免循环引用
    weak var delegate: VisitorViewDelegate?
    
    /**
     设置未登录界面
     
     - parameter isHome:    是否是首页
     - parameter imageName: 需要显示的图片
     - parameter message:   需要显示的文字
     */
    func setupVisitorInfo(isHome: Bool, imageName: String,message: String) {
        // 不是首页 隐藏键盘
        iconView.hidden = !isHome;
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        
        // 是否需要动画
        if isHome {
            startAnimation()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 2.布局
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        // 哪个控件 的 什么属性  等于 另外一个控件 的 什么属性 乘以 多少 加上多少
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        
        // 设置按钮
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x:  0 ,y: 20))
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x:  0 ,y: 20))
        
        // 设置蒙版
//        maskBGView.xmg_AlignInner(type: XMG_AlignType.Center, referView: homeIcon, size: nil)
        maskBGView.xmg_Fill(self)
        
    }
    
    // Swift推荐我们自定义控件,要么用纯代码 要么用xib/stroyboard
    required init?(coder aDecoder: NSCoder) {
        // 如果通过xib/storyboard创建,那么就会崩溃
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 动画
    private func startAnimation() {
        // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 该属性默认为YES,代表动画执行完毕移除 设置不移除
        anim.removedOnCompletion = false
        // 3.将动画添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 懒加载控件
    // 转盘
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    // 图标
    private lazy var homeIcon: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    // 文本
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.text = "asdfjasdfklasdfjasdfklasdfjasdfklasdfjasdfklasdfjasdfklasdfjasdfklasdfjasdfkl;asfd";
        return label
    }()
    ///  登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VisitorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VisitorView.registerBtnClick), forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()
    private lazy var maskBGView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    
    
    //MARK: - 点击按钮
    func registerBtnClick() {
        print(#function)
        delegate?.registerBtnWillClick()
    }
    func loginBtnClick() {
        print(#function)
        delegate?.loginBtnWillClick()
    }
    
    
}
































